using System.Reflection;
using UnityEngine;
using UnityEngine.UI;

public static class UGUIExtensionDefaultControls
{
	public struct Resources
	{
		public Sprite standard;
		public Sprite background;
		public Sprite inputField;
		public Sprite knob;
		public Sprite checkmark;
		public Sprite dropdown;
		public Sprite mask;
	}

	private const float kWidth = 160f;
	private const float kThickHeight = 30f;
	private const float kThinHeight = 20f;
	private static Vector2 s_ThickElementSize = new Vector2(kWidth, kThickHeight);
	private static Vector2 s_ThinElementSize = new Vector2(kWidth, kThinHeight);
	private static Vector2 s_ImageElementSize = new Vector2(100f, 100f);
	private static Color s_DefaultSelectableColor = new Color(1f, 1f, 1f, 1f);
	private static Color s_PanelColor = new Color(1f, 1f, 1f, 0.392f);
	private static Color s_TextColor = new Color(50f / 255f, 50f / 255f, 50f / 255f, 1f);

	// Helper methods at top

	private static GameObject CreateUIElementRoot(string name, Vector2 size)
	{
		GameObject child = new GameObject(name);
		RectTransform rectTransform = child.AddComponent<RectTransform>();
		rectTransform.sizeDelta = size;
		return child;
	}

	static GameObject CreateUIObject(string name, GameObject parent)
	{
		GameObject go = new GameObject(name);
		go.AddComponent<RectTransform>();
		SetParentAndAlign(go, parent);
		return go;
	}

	private static void SetDefaultTextValues(Text lbl)
	{
		lbl.color = s_TextColor;

		System.Type textType = lbl.GetType();
		MethodInfo mi = textType.GetMethod("AssignDefaultFont", BindingFlags.NonPublic | BindingFlags.Instance);
		mi.Invoke(lbl, new System.Object[] { });
	}

	private static void SetDefaultColorTransitionValues(Selectable slider)
	{
		ColorBlock colors = slider.colors;
		colors.highlightedColor = new Color(0.882f, 0.882f, 0.882f);
		colors.pressedColor = new Color(0.698f, 0.698f, 0.698f);
		colors.disabledColor = new Color(0.521f, 0.521f, 0.521f);
	}

	private static void SetParentAndAlign(GameObject child, GameObject parent)
	{
		if (parent == null)
			return;

		child.transform.SetParent(parent.transform, false);
		SetLayerRecursively(child, parent.layer);
	}

	private static void SetLayerRecursively(GameObject go, int layer)
	{
		go.layer = layer;
		Transform t = go.transform;
		for (int i = 0; i < t.childCount; i++)
			SetLayerRecursively(t.GetChild(i).gameObject, layer);
	}

	public static GameObject CreateGUIImage(Resources resources)
	{
		GameObject go = CreateUIElementRoot("GUIImage", s_ImageElementSize);
		go.AddComponent<GUIImage>();
		return go;
	}

}
